package com.itheima.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.itheima.entity.Menu;
import com.itheima.entity.Users;
import com.itheima.mapper.UsersMapper;
import com.itheima.utils.AccessLimit;
import com.itheima.utils.PageEntity;
import com.itheima.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询菜单数
 * @Date :2019-10-16 14:45
 * @Author : yuan_he_song
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private UsersMapper userInfoMapper;


    /**
     * 自己开发的自动装配starter服务
     */
//    @Autowired
//    private HelloService helloService;


    @GetMapping("user_list")
    public JSONObject list(Integer page, Integer size){
        Integer total = userInfoMapper.count();
        List<Users> list = userInfoMapper.list((page-1)*size, size);
        PageEntity<Users> pageEntity = new PageEntity<>(size,total,list);
        return ResultUtils.successData(pageEntity,"SUCCESS");
    }

    /**
     * 一次性获取菜单树数据(递归)
     */
    @GetMapping("selectMenuTree")
    @AccessLimit(limit = 1000, seconds = 60, desc = "递归查询树形菜单.....")
    public List<Menu> selectMenuTree(String menuId, Integer userId){
        // 1.查询所有的菜单数据
        List<Menu> allMenus = Menu.getMenuList();
        // 2.获取所有的一级菜单
        List<Menu> oneLevelMenu = getList();
        for (Menu menu : oneLevelMenu) {
            // 3.为一级菜单设置子菜单，getChild是递归调用的
            List<Menu> children = getChild(menu.getId(), allMenus);
            menu.setChildren(children);
        }
        System.err.println(">>>>>>>正在执行目标方法<<<<<<<<");
        return oneLevelMenu;
    }

    /**
     * 递归获取子菜单
     * @param parentId : 传入的菜单id作为parentId
     * @param allMenus : 所有的菜单
     */
    private List<Menu> getChild(Integer parentId, List<Menu> allMenus) {
        // 每个一级节点的子菜单
        List<Menu> childList = Lists.newArrayList();
        for (Menu menu : allMenus) {
            if(menu.getPid() == parentId){
                childList.add(menu);
                // 以当前菜单节点作为父节点递归获取子菜单
                List<Menu> child = getChild(menu.getId(), allMenus);
                menu.setChildren(child);
            }
        }
        // 递归退出条件
        if(childList.isEmpty()) {
            return childList;
        }
        return childList;
    }


    /**
    * @Description:获取所有的一级菜单
    * @Author: yuan_he_song
    * @Date: 2019-10-16 14:59
    */
    @GetMapping("getAll")
    public List<Menu> getList(){
        List<Menu> menuList = Menu.getMenuList();
        List<Menu> result = new ArrayList<>();
        for (Menu menu : menuList) {
            if(menu.getPid() == 0){
                result.add(menu);
            }
        }
        return result;
    }

    /**
    * 获取每个菜单的子菜单
    * @Author: yuan_he_song
    * @Date: 2019-10-16 14:59
    */
    @GetMapping("selectChildren")
    public  List<Menu> selectChildren(Integer parentId){
        List<Menu> result = new ArrayList<>();
        List<Menu> menuList = Menu.getMenuList();
        for (Menu children : menuList) {
            if(parentId.equals(children.getId())) {
                continue;
            }
            if(parentId.equals(children.getPid())){
                result.add(children);
            }
        }
        return result;
    }










}
